package com.xinchao.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.xinchao.dao.AdvertisementMonitorMapper;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.AdvertisementMonitor;
import com.xinchao.service.AdvertisementMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dxy
 * @date 2018/9/20 16:14
 */
@Service(value = "advertisementMonitorService")
public class AdvertisementMonitorServiceImpl implements AdvertisementMonitorService {
	@Autowired
	private AdvertisementMonitorMapper advertisementMonitorMapper;

	/**
	 * 批量保存广告监播
	 * @param monitorDataJsonArray JSONArray
	 * @throws ServiceException
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveBatchAdvertisementMonitor(JSONArray monitorDataJsonArray) throws ServiceException {
		if (monitorDataJsonArray == null || monitorDataJsonArray.size() == 0){
			throw new ServiceException("输入数据不能为空");
		}
		List<AdvertisementMonitor> advertisementMonitorList = new ArrayList<>();
		for (int i = 0; i < monitorDataJsonArray.size(); i++) {
			AdvertisementMonitor advertisementMonitor = monitorDataJsonArray.getObject(i, AdvertisementMonitor.class);
			advertisementMonitor.setAddTime(System.currentTimeMillis());
			advertisementMonitorList.add(advertisementMonitor);
		}
		advertisementMonitorMapper.insertAdvertisementMonitorBatch(advertisementMonitorList);
	}
}
